//
//  EFAPjtAddTCell.m
//  efairy
//
//  Created by XiangDongMBP on 2017/8/12.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFAPjtAddTCell.h"
#import "EFAModel.h"

@interface EFAPjtAddTCell()<UITextViewDelegate>
@end

@implementation EFAPjtAddTCell
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    [self.valueTextField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.scanBtn addTarget:self action:@selector(scanBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    self.textView.delegate = self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)textFieldEditingChanged:(UITextField *)textField {
    // 文字长度限制
    NSInteger maxLength = self.textMaxLength;
    NSString *toBeString = textField.text;
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position || !selectedRange) {
        if (toBeString.length > maxLength) {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
            if (rangeIndex.length == 1) {
                textField.text = [toBeString substringToIndex:maxLength];
            } else {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(pjtAddTCell:editChanged:)]) {
        [self.delegate pjtAddTCell:self editChanged:textField];
    }
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    NSString *toBeString = textView.text;
    NSInteger maxLength = self.textMaxLength;
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position || !selectedRange) {
        if (toBeString.length > maxLength) {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
            if (rangeIndex.length == 1) {
                textView.text = [toBeString substringToIndex:maxLength];
            } else {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textView.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(pjtAddTCell:textViewDidChanged:)]) {
        [self.delegate pjtAddTCell:self textViewDidChanged:textView];
    }
}

- (void)scanBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(pjtAddTCell:scanBtnTapped:)]) {
        [self.delegate pjtAddTCell:self scanBtnTapped:btn];
    }
}

- (void)configViewWithViewCfg:(EFAPjtAddViewCfg *)cfg {
    self.titleLabel.text = cfg.keyDes;
    self.valueTextField.text = cfg.value;
    self.valueTextField.hidden = !cfg.isTextField;
    self.textView.hidden = cfg.isTextField;
    self.arrowImgView.hidden = cfg.isInput;
    self.arrowWidthConstraint.constant = cfg.isInput ? 0 : 20;
    self.valueTextField.enabled = (cfg.isInput && cfg.enabled);
    self.textView.editable = (cfg.isInput && cfg.enabled);
    self.valueTextField.placeholder = cfg.isInput ? @"请填写" : nil;
    self.textMaxLength = cfg.valueMaxLen;
    self.scanBtn.hidden = !cfg.scanEnabled;
    if (cfg.scanEnabled) {
        self.arrowWidthConstraint.constant = 20;
        self.arrowImgView.hidden = YES;
    }
    if (!cfg.isTextField) {
        self.textView.text = cfg.value;
    }
}

@end
